package com.mzw.huaxiarealestatebackend.controller;
import com.baomidou.mybatisplus.extension.api.R;
import com.mzw.huaxiarealestatebackend.common.utils.DataResult;
import com.mzw.huaxiarealestatebackend.common.utils.DateFormat;
import com.mzw.huaxiarealestatebackend.entity.*;
import com.mzw.huaxiarealestatebackend.service.ProjectRecordService;
import com.mzw.huaxiarealestatebackend.service.StaffService;
import com.mzw.huaxiarealestatebackend.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * (Staff)表控制层
 *
 * @author makejava
 * @since 2023-12-11 16:21:55
 */
@RestController
@RequestMapping("staff")
public class StaffController {
    /**
     * 服务对象
     */
    @Resource
    private StaffService staffService;
    @Autowired
    private ProjectRecordService projectRecordService;
    @Autowired
    private UserRoleService userRoleService;


    /**
     * 分配任务
     * @param pjtNo
     * @param pcsNo
     * @param stfNo
     * @return
     */
    @PostMapping("/allocateTask")
    public DataResult allocateTask(@RequestParam("pjtNo") String pjtNo, @RequestParam("pcsNo") String pcsNo, @RequestParam("stfNo") String stfNo){
        ProjectRecord projectRecord = new ProjectRecord();
        projectRecord.setPjtNo(pjtNo);
        projectRecord.setPcsNo(Integer.parseInt(pcsNo));
        List<ProjectRecord> projectRecords = this.projectRecordService.queryByCondition(projectRecord);
        if (projectRecords.size() != 0){
            ProjectRecord projectRecord1 = projectRecords.get(0);
            projectRecord1.setStfNo(stfNo);
            boolean update = this.projectRecordService.update(projectRecord1);
            if (update) return DataResult.ok("分配成功");
            else return DataResult.failure("分配失败");
        }else {
            ProjectRecord projectRecord2 = new ProjectRecord();
            projectRecord2.setPjtNo(pjtNo);
            projectRecord2.setPcsNo(Integer.parseInt(pcsNo));
            projectRecord2.setStfNo(stfNo);
            projectRecord2.setPrcdTime(DateFormat.getNowDateFormat());
            boolean insert = this.projectRecordService.insert(projectRecord2);
            if (insert) return DataResult.ok("分配成功");
        }
        return DataResult.failure("分配失败");
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public DataResult queryAll() {
        List<Staff> staffs = this.staffService.queryAll();
        for (Staff staff: staffs) {
            staff.resetName();
        }
        return DataResult.ok(staffs);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public DataResult queryById(@PathVariable("id") String id) {
        Staff staff = this.staffService.queryById(id);
        if (staff != null){
            staff.resetName();
            return DataResult.ok(staff);
        }else {
            return DataResult.failure("查找失败");
        }
    }

    /**
     * 模糊查询
     * @param staff
     * @return
     */
    @PostMapping("/queryByCondition")
    public DataResult queryByCondition(@RequestBody Staff staff){
        List<Staff> staffs = this.staffService.queryByCondition(staff);

        for (Staff s :
                staffs) {
            s.resetName();
        }
        return DataResult.ok(staffs);
    }

    /**
     * 查询当前部门的所有员工
     * @param userId
     * @return
     */
    @GetMapping("/queryStaffByUserId")
    public DataResult queryByUserId(@RequestParam("userId") String userId){
        List<Staff> staffs = this.staffService.queryByUserId(userId);
        for (Staff s:
             staffs) {
            s.resetName();
        }
        return DataResult.ok(staffs);
    }

    /**
     * 查询当前部门的所有在职员工
     * @param userId
     * @return
     */
    @GetMapping("/queryStaffByUserIdStatus")
    public DataResult queryByUserIdStatus(@RequestParam("userId") String userId){
        List<Staff> staffs = this.staffService.queryByUserId2(userId);
        for (Staff s1:
                staffs) {
            s1.resetName();
        }
        return DataResult.ok(staffs);
    }


    /**
     * 新增数据
     *
     * @param staff 实体
     * @return 新增结果
     */
    @PostMapping
    public DataResult add(@RequestBody Staff staff) {
        try {
            boolean insert = this.staffService.insert(staff);
            if (insert) {
                return DataResult.ok("新增成功");
            } else {
                return DataResult.failure("员工已存在");
            }
        } catch (DuplicateKeyException e) {
            // 处理主键（唯一约束）冲突
            return DataResult.failure("员工已存在");
        } catch (Exception e) {
            // 处理其他异常
            return DataResult.failure("新增失败，发生异常");
        }
    }

    /**
     * 编辑数据
     *
     * @param staff 实体
     * @return 编辑结果
     */
    @PutMapping
    public DataResult edit(@RequestBody Staff staff) {
        boolean update = this.staffService.update(staff);
        if (update){
            return DataResult.ok("修改成功");
        }else {
            return DataResult.failure("修改失败");
        }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public DataResult deleteById(@RequestParam("id") String id) {
        try {
            boolean success = this.staffService.deleteById(id);
            if (success) {
                return DataResult.ok("删除成功");
            } else {
                return DataResult.failure("删除失败，找不到对应记录");
            }
        } catch (Exception e) {
            // 处理其他异常
            return DataResult.failure("删除失败，发生异常");
        }
    }

}

